package com.netprovider.user.service.baseinfo;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.security.JAASAuthenticationFilter;
import org.apache.cxf.jaxrs.security.SimpleAuthorizingFilter;
import org.apache.cxf.rs.security.oauth2.client.HttpRequestProperties;
import org.apache.cxf.rs.security.oauth2.client.OAuthClientUtils;
import org.apache.cxf.rs.security.oauth2.common.ClientAccessToken;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.netprovider.user.service.baseinfo.objects.User;
import com.netprovider.user.service.baseinfo.objects.Users;

public class UserServiceTestClient {

	public static final String SERVICE_URL = "http://localhost:82/cxf/userservices/v1.0";
	public static final String MIME_TYPE = "application/json";
	public static String TEST_USER_ID = null;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		UserServiceTestClient t = new UserServiceTestClient();
		t.testAddUser();
		t.testGetUsers();
		t.testUpdateUser();
		t.testDeleteUser();
		JAASAuthenticationFilter d;
	}

    @Test
	public void testAddUser() {
        WebClient client = WebClient.create(SERVICE_URL);  
        client.path("/adduser").accept(MIME_TYPE).type(MIME_TYPE);  
        User user = new User();  
        user.setUserId("00" + new Random().nextInt(10000));
        user.setGender("Male");
        user.setNickname("Who am I");
        user.setRegisterDate(new Date());
        User addedUser = client.post(user, User.class);  
        System.out.println("added user id:" + addedUser.getUserId());
        assertResult(user.getUserId(),addedUser.getUserId());
        //set TEST_USER_ID to the one added, so that the user
        //which is added for testing can be deleted later
        TEST_USER_ID = addedUser.getUserId();
        System.out.println("Test addUser method successfully");
	}

    @Test
	public void testGetUsers() {
		   WebClient client = WebClient.create(SERVICE_URL);
		   //according to the tested data we insert,during this time period
		   //only one person registered
	       Users users = client.path("/getusers/startdate/2012-10-17/enddate/2012-10-19")
	                   .accept(MIME_TYPE).type(MIME_TYPE).get(Users.class);  
	       assertResult("1", String.valueOf(users.getUsers().size()));
           System.out.println("Test getUsers method successfully");
	}

    @Test
	public void testUpdateUser() {
		WebClient client = WebClient.create(SERVICE_URL);
	    User user = new User();  
        user.setUserId(TEST_USER_ID);
        user.setGender("Female");
        user.setNickname("Test Nickname");
        user.setRegisterDate(new Date());
        Response response  = client.path("/updateuser")
                       .accept(MIME_TYPE).type(MIME_TYPE).put(user);  
	    assertResult("200", String.valueOf(response.getStatus()));
        System.out.println("Test updateUser method successfully");	
    }

    @Test
	public void testDeleteUser() {
		WebClient client = WebClient.create(SERVICE_URL);
		Response response = client.path("/deleteuser/" + TEST_USER_ID) 
	                         .accept(MIME_TYPE).type(MIME_TYPE).delete(); 
		System.out.println(JSON.toJSONString(response.getEntity()));
		assertResult("200", String.valueOf(response.getStatus()));
        System.out.println("Test deleteuser method successfully");
	}
	
 
    @Test
    public void assertResult(String expectedResult , String realResult) {
    	if (expectedResult != realResult && !expectedResult.equals(realResult)) {
    		throw new RuntimeException("Test failed,excepted result:" 
    				     + expectedResult + " real returned result:" + realResult);
    	}
    }

    @Test
    public void  dd(){
    	String requestURI = "http://localhost:8080/calendar";
    	WebClient wc = WebClient.create(requestURI);
    	 
    	// represents client registration
//    	OAuthClientUtils.Consumer consumer = getConsumer();
//    	// the token issued by AccessTokenService
//    	ClientAccessToken token = getToken();
//    	 
//    	HttpRequestProperties httpProps = new HttpRequestProperties(wc, "GET");
//    	String authHeader = OAuthClientUtils.createAuthorizationHeader(consumer, token, httpProps);
//    	wc.header("Authorization", authHeader);
//    	 
//    	Calendar calendar = wc.get(Calendar.class);
    }
}
